﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        bool isloginSuccess_Bank = false;//判断是否在银行登录成功
        bool isloginSuccess_ATM = false;//判断是否在ATM登录成功
        public Form1()
        {
            InitializeComponent();
        }
        //3、声明事件
        public static event BigMoneyFetchedEventHandler BigMoneyFetched;
        //6、事件处理方法
        public static void alertBigMoney(object sender, BigMoneyArgs e)
        {
            //弹出窗口警告
            MessageBox.Show("在ATM取款不允许超过10000");
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != " " && textBox2.Text != " ")//判断有输入账户密码才创建账户
            {
                Bank.register(textBox1.Text, textBox2.Text);//调用银行注册函数
                textBox1.Clear();
                textBox2.Clear();
                MessageBox.Show("注册成功");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (Account i in Bank.getaccountsList())
            {
                if (textBox1.Text == i.getaccountnumber() && textBox2.Text == i.getpassword())
                {
                    currentAccount = i;//目前执行操作的用户，为一个static 的全局变量
                    textBox2.Clear();
                    isloginSuccess_Bank = true;
                    MessageBox.Show("登录成功");
                    break;
                }
            }
            if (!isloginSuccess_Bank)
            {
                textBox1.Clear();
                textBox2.Clear();
                MessageBox.Show("登录错误");
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (isloginSuccess_Bank)
            {
                textBox5.Text = currentAccount.queryBalanve() + " ";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (isloginSuccess_Bank)
            {
                if (currentAccount.queryBalanve() >= float.Parse(textBox6.Text))
                {
                    try//处理可能抛出的异常
                    {
                        currentAccount.withdraw(float.Parse(textBox6.Text));
                        textBox6.Clear();
                        MessageBox.Show("取款成功");
                    }
                    catch (BadCashException ex)
                    {
                        textBox6.Clear();
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    textBox6.Clear();
                    MessageBox.Show("余额不足");
                }

            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (isloginSuccess_Bank)
            {
                int depositmoney = int.Parse(textBox7.Text);
                currentAccount.deposit((float)depositmoney);
                textBox7.Clear();
                MessageBox.Show("存款成功");
            }

        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (isloginSuccess_ATM)
            {
                if (float.Parse(textBox9.Text) > 10000.0)
                {
                    //4、符合条件则发生一个事件,ATN取款超过10000
                    BigMoneyArgs args = new BigMoneyArgs();
                    args.account = currentAccount;
                    args.fetchedmoney = float.Parse(textBox9.Text);
                    BigMoneyFetched(this, args);

                }
                else if (currentAccount.queryBalanve() >= float.Parse(textBox9.Text))
                {
                    try
                    {
                        currentAccount.withdraw(float.Parse(textBox9.Text));
                        textBox9.Clear();
                        MessageBox.Show("取款成功");
                    }
                    catch (BadCashException ex)
                    {
                        textBox9.Clear();
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    textBox9.Clear();
                    MessageBox.Show("余额不足");
                }

            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (Account i in Bank.getaccountsList())
            {
                if (textBox3.Text == i.getaccountnumber() && textBox4.Text == i.getpassword())
                {
                    currentAccount = i;
                    textBox4.Clear();
                    isloginSuccess_ATM = true;
                    MessageBox.Show("登录成功");
                    break;
                }
            }
            if (!isloginSuccess_ATM)
            {
                textBox3.Clear();
                textBox4.Clear();
                MessageBox.Show("登录错误");
            }

        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (isloginSuccess_ATM)
            {
                textBox8.Text = currentAccount.queryBalanve() + " ";
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (isloginSuccess_ATM)
            {
                int depositmoney = int.Parse(textBox10.Text);
                currentAccount.deposit((float)depositmoney);
                textBox10.Clear();
                MessageBox.Show("存款成功");
            }
        }

    }
}
